package com.bwie.sixth.commerce.service.impl;

import com.bwie.common.utils.R;
import com.bwie.sixth.commerce.controller.UserController;
import com.bwie.sixth.commerce.dao.BuyDao;
import com.bwie.sixth.commerce.dao.UserDao;
import com.bwie.sixth.commerce.entity.BuyEntity;
import com.bwie.sixth.commerce.entity.UserEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bwie.common.utils.PageUtils;
import com.bwie.common.utils.Query;

import com.bwie.sixth.commerce.dao.RecordListDao;
import com.bwie.sixth.commerce.entity.RecordListEntity;
import com.bwie.sixth.commerce.service.RecordListService;


@Service("recordListService")
public class RecordListServiceImpl extends ServiceImpl<RecordListDao, RecordListEntity> implements RecordListService {

    @Autowired
    private UserDao userDao;
    @Autowired
    private BuyDao buyDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<RecordListEntity> page = this.page(
                new Query<RecordListEntity>().getPage(params),
                new QueryWrapper<RecordListEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public String add(RecordListEntity recordListEntity) {
        //判断用户金额是否足够
        UserEntity userEntity = userDao.selectById(recordListEntity.getUserId());
        if (userEntity.getUserPrice().compareTo(recordListEntity.getBuyPrice()) > -1) {
            //钱足够，拼团成功，扣钱
            userEntity.setUserPrice(userEntity.getUserPrice().subtract(recordListEntity.getBuyPrice()));
            //修改用户金钱的信息
            userDao.updateById(userEntity);
            recordListEntity.setRecordStatus(1);
            //添加
            baseMapper.insert(recordListEntity);
            List<RecordListEntity> buy_id = baseMapper.selectList(new QueryWrapper<RecordListEntity>().eq("buy_id", recordListEntity.getBuyId()));
            BuyEntity buyId = buyDao.selectOne(new QueryWrapper<BuyEntity>().eq("buy_id", recordListEntity.getBuyId()));
            if (buy_id.size()==buyId.getBuyNum()){
                for (RecordListEntity listEntity : buy_id) {
                    listEntity.setRecordStatus(0);
                    baseMapper.updateById(listEntity);
                }
            }
        }
        else{
            return "余额不足";
        }
        return "成功";
    }

    //判断活动时间是否过期，过期改变状态，未过期，不改变
    @Scheduled(cron = "0/1 * * * * ?")
    public void stau() {
        List<RecordListEntity> recordStatus = baseMapper.selectList(new QueryWrapper<RecordListEntity>().eq("record_status", 1));
        //对未失效（status=1）活动的拼团活动进行判断
        if (recordStatus.size()!=0){
            for (RecordListEntity status : recordStatus) {
                Date date = new Date();
                if (status.getBuyExprietime().before(date)) {
                    //过期,修改状态
                    status.setRecordStatus(-1);
                    baseMapper.updateById(status);
                }
            }
        }
    }

//    @Scheduled(cron = "0/5 * * * * ?")
//    public void statu() {
//        List<RecordListEntity> recordStatus = baseMapper.selectList(new QueryWrapper<RecordListEntity>().eq("record_status", 1));
//        //对未失效（status=1）活动的拼团活动进行判断
//
//
//    }
}